//
//  IGCommunityViewController.m
//  jingdongfang
//
//  Created by BOE on 2017/8/16.
//  Copyright © 2017年 kevinLY. All rights reserved.
//

#import "IGCommunityVC.h"
#import "FTPopOverMenu.h"
#import "IGCommunityListVC.h"
#import "IGMePubTopicVC.h"
#import "IGCommunitySearchVC.h"
#import "IGCommunityPageApi.h"
#import "IGTopicDetailVC.h"
#import "IGNetReachable.h"
#import "IGEmptyView.h"
#import "IGHotPersonCell.h"

@interface IGCommunityVC ()<UISearchBarDelegate>

@property (nonatomic, strong) NSArray *hotCommunityArr;                 ///>热门圈子

@property (nonatomic, strong) IGCommunityPageApi *communityPageApi;
@property (nonatomic, copy) NSString *lastTimeDate;
@property (nonatomic, assign) NSInteger page;

@end

@implementation IGCommunityVC

#pragma mark -生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isNavgationBarHidden = YES;
    self.heightForFirstSectionCell = 82.0f;
    self.heightForSecondSectionCell = 82.0f;
    
    _lastTimeDate = @"0";
    _page = 1;
    self.communityPageApi = [[IGCommunityPageApi alloc] initWithlastTimeDate:_lastTimeDate
                                                                        page:_page];
    self.requestApi = self.communityPageApi;
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
    }];
    __weak typeof(self) weakSelf = self;
    [self.tableView ig_setHeaderWithRefreshingBlock:^{
        weakSelf.lastTimeDate = @"0";
        weakSelf.page = 1;
        weakSelf.communityPageApi.lastTimeDate = weakSelf.lastTimeDate;
        weakSelf.communityPageApi.page = weakSelf.page;
        
        [weakSelf requestData];
    }];
    
    [self requestData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view endEditing:YES];
}

- (void)requestData {
    __weak typeof(self) weakSelf = self;
    [self.communityPageApi startWithDecodeClass:[IGCommunityPageResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [weakSelf.tableView ig_endHeaderRefreshing];
        [weakSelf.tableView ig_endFooterRefreshing];
        
        if (responseObject) {
            IGCommunityPageResp *resp = responseObject;
            IGCommunityPageModel *model = resp.data;
            
            NSMutableArray *tmpTopicArr = model.topics;
            if (tmpTopicArr && tmpTopicArr.count > 0) {
                [weakSelf setupFooter];
            } else {
                [weakSelf.tableView ig_endFooterRefreshingWithNoMoreData];
            }
            
            if (weakSelf.page == 1) {
                weakSelf.hotCommunityArr = [NSArray arrayWithArray:model.hotCircles];
                weakSelf.dataSource = tmpTopicArr;
            } else {
                [weakSelf.dataSource addObjectsFromArray:tmpTopicArr];
            }
            weakSelf.lastTimeDate = model.lastTopicTime;
        }
        
        [weakSelf.tableView reloadData];
    }];
}

- (void)setupFooter {
    __weak typeof(self) weakSelf = self;
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        weakSelf.communityPageApi.lastTimeDate = weakSelf.lastTimeDate;
        weakSelf.page++;
        weakSelf.communityPageApi.page = weakSelf.page;
        
        [weakSelf requestData];
    }];
}

#pragma mark - Delegate
- (CGFloat)heightForFirstSectionCell {
    if (self.hotCommunityArr.count != 0) {
        return 82.f + 50.f;
    }
    return 0.1f;
}

- (CGFloat)heightForSecondSectionCell {
//    if (self.hotCommunityArr.count != 0) {
//        return 88.f + 50.f;
//    }
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return 5.f;
    }
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 5)];
        view.backgroundColor = [UIColor mainBackgroundColor];
        return view;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isShowKeyBoard) {
        [self.view endEditing:YES];
        return;
    }
    
    IGTopicModel *model = [self.dataSource objectAtIndex:indexPath.row];
    IGTopicDetailVC *detailVC = [[IGTopicDetailVC alloc] initWithTopicId:model.tid];
    [UIViewController ig_pushViewController:detailVC animated:YES];
}

- (UITableViewCell *)customFirstSectionCell:(UITableView *)tableView {
    IGHotCommunityCell *cell = [IGHotCommunityCell cellWithTableView:tableView];
    cell.hotCommunityArr = _hotCommunityArr;
    
    return cell;
}

//- (UITableViewCell *)customSecondSectionCell:(UITableView *)tableView {
//    IGHotPersonCell *cell = [IGHotPersonCell cellWithTableView:tableView];
//    cell.hotPersonArr = _hotCommunityArr;
//
//    return cell;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
